home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 3d_lib.zip / 3D.H next >
Text File  |  1993-05-09  |  7KB  |  175 lines

  1. /* 3D TRANSFORMS defines and prototypes
  2.  
  3.    Copyright (c) 1988 by Gus O'Donnell
  4.  
  5.    Revision history:
  6.  
  7.    Version 1.00         February 29, 1988       As released.
  8.  
  9.    Version 1.01         March 20, 1988          Created libraries for all
  10.                                                 memory models
  11.  
  12.  
  13.     3-D graphics library data structures and prototypes.
  14.     These functions are based on information in
  15.     "Principles of Interactive Computer Graphics", by
  16.     Newman and Sproull, Second Edition, McGraw-Hill,
  17.     Publishers.
  18. */
  19.  
  20. #define    CMAX    4    /* maximum column (4 x 4 matrix) */
  21. #define    RMAX    4    /* maximum row    (4 x 4 matrix) */
  22. #define    DIM     3    /* number of dimensions */
  23.  
  24. typedef double MATRIX [RMAX] [CMAX];
  25.  
  26. /* A MATRIX is used to describe a transformation from one coordinate
  27. system to another.  Multiple transformations may be concatenated by
  28. multiplying their transformation matrices. */
  29.  
  30. typedef double VECTOR [DIM];
  31.  
  32. /* A VECTOR is an array of three coordinates [x,y,z] representing magnitude
  33. and direction.  It is typically used to represent the direction of a
  34. light source, or the normal of a plane. */
  35.  
  36. typedef struct vertex {
  37.     double coord [DIM];
  38.     struct vertex *next;
  39. }VERTEX;
  40.  
  41. /* A VERTEX consists of a point in three dimensions (coord[DIM]), plus
  42. a pointer.  Vertices are arranged in linked lists attached to objects.
  43. An object is transformed (rotated, translated, or scaled) by transforming
  44. its vertex list. */
  45.  
  46. typedef struct corner {
  47.     VERTEX *this;
  48.     struct corner *next;
  49. }CORNER;
  50.  
  51. /* A CORNER is merely a pointer to a VERTEX, plus a pointer to the next
  52. CORNER in a linked list.  Corners are arranged in linked lists attached to
  53. faces. */
  54.  
  55. typedef struct face {
  56.     CORNER *first;
  57.     struct face *next;
  58. }FACE;
  59.  
  60. /* A FACE is a plane figure consisting of a linked list of corners.
  61. The corners in the list are assumed to be in clockwise order as viewed
  62. from the outside of an object. */
  63.  
  64. typedef struct object {
  65.     FACE *faces;
  66.     VERTEX *vertices;
  67. } OBJECT;
  68.  
  69. /* An OBJECT is a solid consisting of faces and vertices.  The topology
  70. is completely described by the list of faces; the geometry is described
  71. by the faces and vertices.  A separate list of vertices is maintained to
  72. reduce the computational overhead during transformation; since many faces
  73. may point to a single vertex, each vertex need be transformed once for the
  74. whole object.
  75.  
  76. The complete data structure looks like this:
  77.  
  78.            OBJECT o o--------------------------------->VERTEX o
  79.                   |                                           |
  80.                   +-->FACE o X                                |
  81.                            |                                  |
  82.                         +--+                                  |
  83.                         |                                     |
  84.                         V                                     |
  85.                       FACE o o---->CORNER o X            +----+
  86.                            |              |              |
  87.                         +--+          +---+              |
  88.                         |             |                  |
  89.                         |             V                  V
  90.                         |          CORNER o o--------->VERTEX o
  91.                         |                 |                   |
  92.                         |             +---+              +----+
  93.                         |             |                  |
  94.                         |             V                  V
  95.                         |          CORNER o o--------->VERTEX o
  96.                         |                 |                   |
  97.                         |             +---+               +----+
  98.                         |             |                   |
  99.                         |             V                   V
  100.                         |          CORNER X o--------->VERTEX o
  101.                         |                 |                   |
  102.                         |             +---+              +----+
  103.                         |             |                  |
  104.                         |             V                  |
  105.                         |          CORNER X X            |
  106.                         V                                |
  107.                       FACE o o---->CORNER o X            |
  108.                            |              |              |
  109.                         +--+          +---+              |
  110.                         |             |                  |
  111.                         V             V                  V
  112.                       FACE X X     CORNER o o--------->VERTEX o
  113.                                           |                   |
  114.                                       +---+              +----+
  115.                                       |                  |
  116.                                       V                  V
  117.                                    CORNER o o--------->VERTEX o
  118.                                           |                   |
  119.                                       +---+              +----+
  120.                                       |                  |
  121.                                       V                  V
  122.                                    CORNER o o--------->VERTEX o
  123.                                           |                   |
  124.                                       +---+              +----+
  125.                                       |                  |
  126.                                       V                  V
  127.                                    CORNER X X          VERTEX X
  128.  
  129. This structure represents an object with two faces of three corners each.
  130. 'X' is the NULL pointer.  In an actual object, more than one CORNER may
  131. point to a given VERTEX.  Thus, when an object is transformed, each VERTEX
  132. need be transformed only once. */
  133.  
  134. /* Initialization functions */
  135.  
  136. void    identity (MATRIX this_mat);
  137. int     new_face (FACE *this_face);
  138. int     new_obj (OBJECT *this_obj);
  139.  
  140. /* Vector and matrix math functions */
  141.  
  142. double  dot_prod (VECTOR vec1, VECTOR vec2);
  143. void    mat_mul (MATRIX mat1, MATRIX mat2, MATRIX prod);
  144. int     normal (FACE *this_face, VECTOR norm);
  145. void    vec_mul (VERTEX *this_vec, MATRIX this_mat, VERTEX *prod);
  146.  
  147. /* Transformation functions */
  148.  
  149. void    scale (double sx, double sy, double sz, MATRIX this_mat);
  150. void    trans (double tx, double ty, double tz, MATRIX this_mat);
  151. void    xrot (double theta, MATRIX this_mat);
  152. void    yrot (double theta, MATRIX this_mat);
  153. void    zrot (double theta, MATRIX this_mat);
  154. void    persp (double s, double d, double f, MATRIX this_mat);
  155.  
  156. /* Put figures on the screen */
  157.  
  158. void    disp_face (VECTOR lsource, int color, FACE *this_face, MATRIX xfrm_mat);
  159. void    disp_object (VECTOR lsource, int color, OBJECT *this_obj, MATRIX xfrm_mat);
  160.  
  161. /* Manipulate data structures */
  162.  
  163. int     add_corner (double x, double y, double z, FACE *this_face);
  164. int     xform (OBJECT this_obj, MATRIX transform);
  165. int     del_face (OBJECT *this_obj, FACE *this_face);
  166. double  max_z (FACE this_face);
  167. double  min_z (FACE this_face);
  168. int     add_face (OBJECT *this_obj, FACE *this_face);
  169.  
  170. /* Dump data structures to screen */
  171.  
  172. void    dump_mat (MATRIX this_mat);
  173. void    dump_vec (VERTEX this_vec);
  174. void    dump_face (FACE this_face);
  175. void    dump_obj (OBJECT this_obj);